package cn.caskm.souvenirshopsystem.service.impl;

import cn.caskm.souvenirshopsystem.constants.BizConstants;
import cn.caskm.souvenirshopsystem.domain.Result;
import cn.caskm.souvenirshopsystem.domain.dto.PageDTO;
import cn.caskm.souvenirshopsystem.domain.po.GoodsCategory;
import cn.caskm.souvenirshopsystem.domain.query.PageQuery;
import cn.caskm.souvenirshopsystem.exceptions.BizException;
import cn.caskm.souvenirshopsystem.mapper.GoodsCategoryMapper;
import cn.caskm.souvenirshopsystem.service.GoodsCategoryService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements GoodsCategoryService {

    @Override
    public Result<PageDTO<GoodsCategory>> page(PageQuery query) {
        // 获取参数
        Long pageNum = query.getPageNum();
        if (pageNum == null) {
            pageNum = BizConstants.DEFAULT_PAGE_NUM;
        }
        Long pageSize = query.getPageSize();
        if (pageSize == null) {
            pageSize = BizConstants.DEFAULT_PAGE_SIZE;
        }

        // 查询数据
        Page<GoodsCategory> page = lambdaQuery()
                .orderByAsc(GoodsCategory::getOrderNum)
                .page(new Page<>(pageNum, pageSize));
        
        List<GoodsCategory> records = page.getRecords();
        return Result.success(PageDTO.of(records, page.getPages(), page.getTotal()));
    }
    
    @Override
    public Result<List<GoodsCategory>> getAll() {
        List<GoodsCategory> list = lambdaQuery()
                .orderByAsc(GoodsCategory::getOrderNum)
                .list();
        return Result.success(list);
    }

    @Override
    public Result<GoodsCategory> detail(Integer id) {
        GoodsCategory goodsCategory = getById(id);
        if (goodsCategory == null) {
            throw new BizException("分类信息不存在");
        }
        return Result.success(goodsCategory);
    }

    @Override
    public void saveOrUpdateEntity(GoodsCategory goodsCategory) {
        if (goodsCategory.getCategoryId() == null) {
            // 新增分类
            if (StrUtil.isNotBlank(goodsCategory.getCategoryName())) {
                Long count = lambdaQuery()
                        .eq(GoodsCategory::getCategoryName, goodsCategory.getCategoryName())
                        .count();
                if (count > 0) {
                    throw new BizException("分类名称已存在");
                }
            }
            save(goodsCategory);
        } else {
            // 修改分类
            if (StrUtil.isNotBlank(goodsCategory.getCategoryName())) {
                Long count = lambdaQuery()
                        .eq(GoodsCategory::getCategoryName, goodsCategory.getCategoryName())
                        .ne(GoodsCategory::getCategoryId, goodsCategory.getCategoryId())
                        .count();
                if (count > 0) {
                    throw new BizException("分类名称已存在");
                }
            }
            updateById(goodsCategory);
        }
    }
}
